import java.util.Arrays;
import java.util.function.IntBinaryOperator;
import java.util.function.IntUnaryOperator;

/**
 * Array类的常用方法
 *
 * @author hongchen
 *         create 2017-03-09-10:12
 */
public class TestArray {

    public static void main(String[] args) {
        int[] a1 = {3, 4, 5, 6};
        int[] a2 = {3, 4, 5, 6};
        System.out.println("a1,a2是否相等：" + Arrays.equals(a1, a2));

        int b[] = Arrays.copyOf(a1, 5);
        System.out.println("复制过来的b是：" + Arrays.toString(b));

        Arrays.fill(b, 4, 5, 1);
        System.out.println("填充之后的b：" + Arrays.toString(b));

        Arrays.sort(b);
        System.out.println("排序之后的b：" + Arrays.toString(b));

        //一下是Java8新增的一些并行处理Array的方法

        int[] arr1 = new int[]{3, -4, 25, 16, 30, 18};
        //增加并行能力的sort方法
        Arrays.parallelSort(arr1);
        System.out.println(Arrays.toString(arr1));

        int[] arr2 = new int[]{3, -4, 25, 16, 30, 18};
        Arrays.parallelPrefix(arr2, new IntBinaryOperator() {//可以用Lambda简化
            //left代表前一个索引处的元素（默认1），right代表当前索引处的元素
            @Override
            public int applyAsInt(int left, int right) {
                return left * right;
            }
        });
        System.out.println(Arrays.toString(arr2));

        int[] arr3 = new int[5];
        Arrays.parallelSetAll(arr3, new IntUnaryOperator() {
            //operand代表当前元素的所引
            @Override
            public int applyAsInt(int operand) {
                return operand*5;
            }
        });
        System.out.println(Arrays.toString(arr3));

    }


}
